/*
 * Copyright (c) 2025 CAX Conversion Project
 */
package com.comac.ins.isfd.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.comac.ins.common.core.exception.base.BaseException;
import com.comac.ins.common.core.utils.MapstructUtils;
import com.comac.ins.common.excel.utils.ExcelUtil;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.isfd.constant.enums.IsfdMetadataTableNameEnum;
import com.comac.ins.isfd.domain.IsfdMetalMaterial;
import com.comac.ins.isfd.domain.bo.IsfdMetalMaterialBo;
import com.comac.ins.isfd.domain.bo.query.IsfdMetalMaterialQueryBo;
import com.comac.ins.isfd.domain.vo.IsfdMetalMaterialVo;
import com.comac.ins.isfd.mapper.IsfdMetalMaterialMapper;
import com.comac.ins.isfd.service.IIsfdMetadataImportService;
import com.comac.ins.isfd.service.IIsfdMetalMaterialService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author Lion Li
 * @date 2024-08-02
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class IsfdMetalMaterialServiceImpl implements IIsfdMetalMaterialService, IIsfdMetadataImportService {

    @Autowired
    private final IsfdMetalMaterialMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 主键
     * @return 【请填写功能名称】
     */
    @Override
    public IsfdMetalMaterialVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询【请填写功能名称】列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 【请填写功能名称】分页列表
     */
    @Override
    public TableDataInfo<IsfdMetalMaterialVo> queryPageList(IsfdMetalMaterialQueryBo bo) {
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNum(bo.getPageNum());
        pageQuery.setPageSize(bo.getPageSize());
        LambdaQueryWrapper<IsfdMetalMaterial> lqw = buildQueryWrapper(bo);
        Page<IsfdMetalMaterialVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的【请填写功能名称】列表
     *
     * @param bo 查询条件
     * @return 【请填写功能名称】列表
     */
    @Override
    public List<IsfdMetalMaterialVo> queryList(IsfdMetalMaterialQueryBo bo) {
        LambdaQueryWrapper<IsfdMetalMaterial> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IsfdMetalMaterial> buildQueryWrapper(IsfdMetalMaterialQueryBo bo) {
        LambdaQueryWrapper<IsfdMetalMaterial> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getMaterialName()), IsfdMetalMaterial::getMaterialName, bo.getMaterialName());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialNumber()), IsfdMetalMaterial::getMaterialNumber, bo.getMaterialNumber());
        lqw.eq(bo.getE() != null, IsfdMetalMaterial::getE, bo.getE());
        lqw.eq(bo.getNu() != null, IsfdMetalMaterial::getNu, bo.getNu());
        lqw.eq(bo.getG() != null, IsfdMetalMaterial::getG, bo.getG());
        lqw.eq(bo.getRho() != null, IsfdMetalMaterial::getRho, bo.getRho());
        lqw.eq(bo.getThmCooe() != null, IsfdMetalMaterial::getThmCooe, bo.getThmCooe());
        lqw.in(CollectionUtil.isNotEmpty(bo.getMetadataManagementIds()), IsfdMetalMaterial::getMetadataManagementId, bo.getMetadataManagementIds());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param bo 【请填写功能名称】
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(IsfdMetalMaterialBo bo) {
        IsfdMetalMaterial add = MapstructUtils.convert(bo, IsfdMetalMaterial.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param bo 【请填写功能名称】
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(IsfdMetalMaterialBo bo) {
        IsfdMetalMaterial update = MapstructUtils.convert(bo, IsfdMetalMaterial.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IsfdMetalMaterial entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除【请填写功能名称】信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public IsfdMetalMaterialVo queryByMaterialNumber(String materialNumber) {
        return baseMapper.selectVoOne(new LambdaQueryWrapper<IsfdMetalMaterial>().eq(IsfdMetalMaterial::getMaterialNumber, materialNumber));
    }

    @Override
    public String selectByTableName() {
        return IsfdMetadataTableNameEnum.ISFD_METAL_MATERIAL.getValue();
    }

    @Override
    public Long excelImport(File excelImportFile, String metadataManagementId) {
        long successCount = 0L;
        try (InputStream is = new FileInputStream(excelImportFile)) {
            List<IsfdMetalMaterialVo> targetVo = ExcelUtil.importExcel(is, IsfdMetalMaterialVo.class);
            for (IsfdMetalMaterialVo vo : targetVo) {
                IsfdMetalMaterialBo bo = MapstructUtils.convert(vo, IsfdMetalMaterialBo.class);

                try{
                    bo.setMetadataManagementId(metadataManagementId);
                    insertByBo(bo);
                    successCount++;
                } catch (Exception e) {
                    log.error("金属材料数据导入失败", e);
                }
            }

        } catch (Exception e) {
            log.error("sheet文件转换为实体类失败", e);
            throw new BaseException("sheet文件转换为实体类失败");
        }

        return successCount;
    }
}
